﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using TimedText;
using TimedText.Styling;

namespace CCAI.Office.PowerPoint.TimedText.Custom
{
    public class TimedTextElementCustom
    {
        public string Id { get; set; }
        public TimeSpan Begin { get; set; }
        public TimeSpan End { get; set; }
        public String Content { get; set; }

        public TimedTextElementTypeCustom ElementType { get; set; }



        public TimedTextStyleCustom Style { get; set; }
    }

    public class CaptionElementCustom : TimedTextElementCustom
    {
        public CaptionElementCustom()
        {
            ElementType = TimedTextElementTypeCustom.Text;
        }
    }

    public enum TimedTextElementTypeCustom
    {
        None,
        Container,
        Text,
        LineBreak,
        Region,
        Animation
    }

    public class TimedTextStyleCustom
    {
        private static readonly Color DefaultColor = Color.White;
        private static readonly Color DefaultBackgroundColor = Color.Black;

        private readonly IList<string> styleAttributes;

        public TimedTextStyleCustom()
        {
            styleAttributes = new List<string>();

            BackgroundColor = DefaultBackgroundColor;
            Color = DefaultColor;
        }

        public string Id { get; set; }
        public Color BackgroundColor { get; set; }
        public Color Color { get; set; }


    }



    public class TimedTextStyleParserCustom
    {
        private static readonly TimedTextStyleCustom DefaultStyle = new TimedTextStyleCustom();
        static ColorConverter converter = new ColorConverter();

        #region IsValidAnimationPropertyName
        internal static bool IsValidAnimationPropertyName(string name)
        {
            bool ret = false;
            switch (name)
            {
                case "backgroundColor":
                    ret = true;
                    break;
                case "color":
                    ret = true;
                    break;
                case "displayAlign":
                    ret = true;
                    break;
                case "display":
                    ret = true;
                    break;
                case "extent":
                    ret = true;
                    break;
                case "fontFamily":
                    ret = true;
                    break;
                case "fontSize":
                    ret = true;
                    break;
                case "fontStyle":
                    ret = true;
                    break;
                case "fontWeight":
                    ret = true;
                    break;
                case "lineHeight":
                    ret = true;
                    break;
                case "opacity":
                    ret = true;
                    break;
                case "origin":
                    ret = true;
                    break;
                case "overflow":
                    ret = true;
                    break;
                case "padding":
                    ret = true;
                    break;
                case "showBackground":
                    ret = true;
                    break;
                case "textAlign":
                    ret = true;
                    break;
                case "visibility":
                    ret = true;
                    break;
                case "wrapOption":
                    ret = true;
                    break;
                case "zIndex":
                    ret = true;
                    break;
            }
            return ret;
        }
        #endregion


        public static TimedTextStyleCustom MapStyle(TimedTextElementBase element, RegionElement region)
        {
            var style = new TimedTextStyleCustom()
            {
                Id = element.Id
            };


            var backgroundColor =
                element.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.BackgroundColor.LocalName, region);

            style.BackgroundColor = backgroundColor != null
                                        ? (Color)converter.ConvertFromString(backgroundColor.ToString())
                                        : DefaultStyle.BackgroundColor;

            var color =
                element.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Color.LocalName, region);
            style.Color = color != null
                              ? (Color)converter.ConvertFromString(color.ToString())
                              : DefaultStyle.Color;

            object oFontSize = element.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.FontSize.LocalName,
                                                            region);
            //var fontSize = oFontSize as string;
            //if (!fontSize.IsNullOrWhiteSpace())
            //{
            //    var parsedFontSize = new NumberPair(fontSize);
            //    style.FontSize = new Length
            //    {
            //        Unit = ConvertEnum<Unit, LengthUnit>(parsedFontSize.UnitMeasureHorizontal),
            //        Value = parsedFontSize.First
            //    };
            //}

            return style;

        }
    }
}
